package cn.mumzone.pos.ui.widgets;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.PayListEntity;
import cn.mumzone.pos.util.BillUtil;

/**
 * Created by qiuzhenhuan on 2019/3/28.
 */

public class PaySummaryView extends LinearLayout {

    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_sum)
    TextView tvSum;
    @BindView(R.id.tv_return_sale_num)
    TextView tvReturnSaleNum;
    @BindView(R.id.tv_return_sale_amount)
    TextView tvReturnSaleAmount;
    @BindView(R.id.tv_calcu_sum_amount)
    TextView tvCalcuSumAmount;
    @BindView(R.id.item_layout)
    LinearLayout itemLayout;
    @BindView(R.id.view_line)
    View viewLine;

    private Context context;

    public PaySummaryView(Context context) {
        this(context, null);
    }

    public PaySummaryView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public PaySummaryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.item_pay_list, this);
        ButterKnife.bind(this);
    }

    public void bindData(PayListEntity payListEntity, boolean isLastItem) {
        int titleColor = context.getResources().getColor(android.R.color.white);
        int countColor = context.getResources().getColor(R.color.navigation_color);
        int moneyColor = context.getResources().getColor(R.color.title_text_color);
        int zeroColor = context.getResources().getColor(R.color.content_text_color);

        if (null == payListEntity) { // 标题：背景黑色，文字白色18sp
            tvPayType.setText("支付方式");
            tvCount.setText("结账次数");
            tvSum.setText("支付总额");
            tvReturnSaleNum.setText("退货次数");
            tvReturnSaleAmount.setText("退款金额");
            tvCalcuSumAmount.setText("合计");

            itemLayout.setBackgroundColor(countColor);
            tvPayType.setTextColor(titleColor);
            tvCount.setTextColor(titleColor);
            tvSum.setTextColor(titleColor);
            tvReturnSaleNum.setTextColor(titleColor);
            tvReturnSaleAmount.setTextColor(titleColor);
            tvCalcuSumAmount.setTextColor(titleColor);
            tvPayType.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvCount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvSum.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvReturnSaleNum.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvReturnSaleAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            tvCalcuSumAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        } else {
            tvPayType.setText(BillUtil.getSimplePayType(payListEntity.getPayType()));
            tvCount.setText(String.valueOf(StringUtils.getInt(payListEntity.getCount())));
            tvSum.setText(StringUtils.getFormattedMoneyWithUnit(payListEntity.getSum()));
            tvReturnSaleNum.setText(String.valueOf(StringUtils.getInt(payListEntity.getReturnSaleNum())));
            tvReturnSaleAmount.setText(StringUtils.getFormattedMoneyWithUnit(payListEntity.getReturnSaleAmount()));
            tvCalcuSumAmount.setText(StringUtils.getFormattedMoneyWithUnit(payListEntity.getCalcuSumAmount()));

            tvCount.setTextColor(StringUtils.getDouble(payListEntity.getCount()) == 0 ? zeroColor : countColor);
            tvSum.setTextColor(StringUtils.getDouble(payListEntity.getSum()) == 0 ? zeroColor : moneyColor);
            tvReturnSaleNum.setTextColor(StringUtils.getDouble(payListEntity.getReturnSaleNum()) == 0 ? zeroColor : countColor);
            tvReturnSaleAmount.setTextColor(StringUtils.getDouble(payListEntity.getReturnSaleAmount()) == 0 ? zeroColor : moneyColor);

            if (isLastItem) {
                tvPayType.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                tvPayType.setTypeface(Typeface.DEFAULT_BOLD);
                tvCalcuSumAmount.setTextColor(context.getResources().getColor(R.color.toast_alert_level_one_text));
            } else {
                tvCalcuSumAmount.setTextColor(StringUtils.getDouble(payListEntity.getCalcuSumAmount()) == 0 ? zeroColor : moneyColor);
                tvPayType.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                tvPayType.setTypeface(Typeface.DEFAULT);
            }
        }
        viewLine.setVisibility(!isLastItem ? View.VISIBLE : View.GONE);
    }
}
